package com.jade.file.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jade.common.annotation.Log;
import com.jade.common.enums.BusinessType;
import com.jade.file.domain.TFileUpload;
import com.jade.file.service.ITFileUploadService;
import com.jade.common.core.controller.BaseController;
import com.jade.common.core.domain.AjaxResult;
import com.jade.common.utils.StringUtils;
import com.jade.common.utils.file.FileUtils;
import com.jade.common.utils.poi.ExcelUtil;
import com.jade.common.core.page.TableDataInfo;

/**
 * 文件模块Controller
 * 
 * @author jade
 * @date 2020-05-09
 */
@Controller
@RequestMapping("/file")
public class TFileUploadController extends BaseController
{
    private String prefix = "file";

    @Autowired
    private ITFileUploadService tFileUploadService;


    @RequiresPermissions("file:file:view")
    @GetMapping()
    public String file()
    {
        return prefix+"/file" ;
    }

    
    /**
     * 查询文件模块列表
     */
    @RequiresPermissions("file:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TFileUpload tFileUpload)
    {
        startPage();
        List<TFileUpload> list = tFileUploadService.selectTFileUploadList(tFileUpload);
        return getDataTable(list);
    }

    /**
     * 导出文件模块列表
     */
    @RequiresPermissions("file:file:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TFileUpload tFileUpload)
    {
        List<TFileUpload> list = tFileUploadService.selectTFileUploadList(tFileUpload);
        ExcelUtil<TFileUpload> util = new ExcelUtil<TFileUpload>(TFileUpload.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 新增文件模块
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文件模块
     */
    @RequiresPermissions("file:file:add")
    @Log(title = "文件模块", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TFileUpload tFileUpload)
    {
        return toAjax(tFileUploadService.insertTFileUpload(tFileUpload));
    }

    /**
     * 修改文件模块
     */
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Long fileId, ModelMap mmap)
    {
        TFileUpload tFileUpload = tFileUploadService.selectTFileUploadById(fileId);
        mmap.put("tFileUpload", tFileUpload);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件模块
     */
    @RequiresPermissions("file:file:edit")
    @Log(title = "文件模块", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TFileUpload tFileUpload)
    {
        return toAjax(tFileUploadService.updateTFileUpload(tFileUpload));
    }

    /**
     * 删除文件模块
     */
    @RequiresPermissions("file:file:remove")
    @Log(title = "文件模块", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tFileUploadService.deleteTFileUploadByIds(ids));
    }
    /**
     * 通用下载请求
     * 
     * @param fileName 文件名称
     * @param delete 是否删除
     */
    @GetMapping("download")
    public void fileDownload(String fileId, HttpServletResponse response, HttpServletRequest request)
    {
        try
        {
            try {
				if (StringUtils.isEmpty(fileId)||Long.valueOf(fileId)==0)
				{
				    throw new Exception(StringUtils.format("缺少参数。 ", fileId));
				}
			} catch (Exception e) {
				//fileid无法变LONG
				throw new Exception(StringUtils.format("fileid不合法。 ", fileId));
			}
          
            TFileUpload selectTFileUploadById = tFileUploadService.selectTFileUploadById(Long.valueOf(fileId));
            String realFileName = System.currentTimeMillis() + selectTFileUploadById.getFileName();
            String filePath = selectTFileUploadById.getFilePath();

            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
            FileUtils.writeBytes(filePath, response.getOutputStream());
        }
        catch (Exception e)
        {
        }
    }
}
